package cn.itcast.controller.cargo;

import cn.itcast.base.BaseController;
import cn.itcast.domain.cargo.ContractExample;
import cn.itcast.domain.cargo.Export;
import cn.itcast.domain.cargo.Packing;
import cn.itcast.domain.cargo.PackingExample;
import cn.itcast.service.ExportService;
import cn.itcast.service.PackingService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.*;

@Controller
@RequestMapping("/cargo/packing")
public class PackingController extends BaseController {

    @Reference
    private PackingService packingService;

    @Reference
    private ExportService exportService;

    @RequestMapping("/packingTo")
    public String packingTo(String id) {
        ArrayList<Export> al = new ArrayList();
        System.out.println(id);
        Packing packing = new Packing();
        double zongTiJi = 0; //体积
        double zongMaoZhong = 0; // 毛重
        double zongJingzhong = 0; //净重
        double zongFeiYong = 0;  //装箱
        String hetonghao = "";  //合同号
        Map map = new HashMap<>();
        if (id != null && id != "") {
            String[] ids = id.split(",");
            for (String iid : ids) {
                Export export = exportService.findById(iid);
                if (export.getMeasurements() != null && export.getMeasurements() != 0) {
                    zongTiJi += export.getMeasurements();
                }
                if (export.getGrossWeights() != null && export.getGrossWeights() != 0.0) {
                    zongMaoZhong += export.getGrossWeights();
                }
                //总净重不知道计算方式
//                if (export.getMeasurements() != null && export.getMeasurements() != 0.0) {
//                    zongJingzhong += export.getMeasurements();
//                }
                if (export.getGrossWeights() != null && export.getGrossWeights() != 0.0) {
                    zongFeiYong += export.getGrossWeights() * 520;
                }
                if (export.getId() != null && export.getId() != "") {
                    hetonghao += export.getId() + " ";
                }
                al.add(export);
                map.put(export.getShipmentPort(), "1");
                map.put(export.getDestinationPort(), "2");
                map.put(export.getConsignee(), "3");
            }
        }
        packing.setGrossWeights(zongMaoZhong);
        packing.setCompanyId(company_id);
        packing.setCompanyName(company_name);
        packing.setCreateTime(new Date());
        packing.setExportNos(hetonghao);
        //总净重不知道计算方式
//        packing.setNetWeights();
        packing.setTotalVolume(zongTiJi);
        packing.setPackingMoney(zongFeiYong);

        int size = map.size();
        if (size != 3) {
            request.setAttribute("msg", "Inconsistent information!");
            return "error2";
        }
        request.setAttribute("packing", packing);
        request.setAttribute("packingId", id);
        request.setAttribute("exports", al);
        return "/cargo/packing/packing-add";
    }

    @RequestMapping("/list")
    public String findAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize) {

        PackingExample packingExample = new PackingExample();
        packingExample.setOrderByClause("create_time desc");
        PackingExample.Criteria criteria = packingExample.createCriteria();
        criteria.andCompanyIdEqualTo(company_id);
        PageInfo page = packingService.findAll(packingExample, pageNum, pageSize);
        request.setAttribute("pb", page);
        return "cargo/packing/packing-list";
    }

    @RequestMapping("/edit")
    public String edit(Packing packing, String packingId) {
        packing.setPackingListId(UUID.randomUUID().toString());
        packing.setCompanyId(company_id);
        packing.setPackingTime(new Date());
        packing.setCompanyName(company_name);
        packingService.save(packing);
        System.out.println(packingId);
        return "redirect:/cargo/packing/list.do";
    }

    @RequestMapping("/delete")
    public String delete(String id) {
        Packing packing = packingService.findById(id);
        Integer state = packing.getState();
        if (state != 0) {
            request.setAttribute("msg", "Can not be deleted!");
            return "error2";
        }
        packingService.delete(id);
        return "redirect:/cargo/packing/list.do";
    }

    @RequestMapping("/submit")
    public String submit(String id) {
        Packing packing = packingService.findById(id);
        request.setAttribute("packing", packing);
        String exportNos = packing.getExportNos();
        ArrayList<Export> al = new ArrayList();
        if (exportNos != null && exportNos != "") {
            String[] ids = exportNos.split(" ");
            for (String iid : ids) {
                Export export = exportService.findById(iid);
                al.add(export);
            }
        }
        request.setAttribute("exports", al);
        request.setAttribute("packing", packing);
        return "/cargo/packing/packing-add";
    }
}
